package cn.com.pajx.pajx_visitor_v3.ui.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;

import com.shawnlin.numberpicker.NumberPicker;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.pajx.pajx_visitor_v3.R;
import cn.com.pajx.pajx_visitor_v3.base.BaseFragment;

/**
 * 学择拜访时间页面
 * create an instance of this fragment.
 */
@SuppressLint("NonConstantResourceId")
public class ChooseTimeFragment extends BaseFragment {

    @BindView(R.id.year)
    NumberPicker year;
    @BindView(R.id.month)
    NumberPicker month;
    @BindView(R.id.day)
    NumberPicker day;
    @BindView(R.id.hour)
    NumberPicker hour;
    @BindView(R.id.minute)
    NumberPicker minute;

    private OnFunctionListener onFunctionListener;

    public ChooseTimeFragment() {
    }

    public static ChooseTimeFragment newInstance() {
        ChooseTimeFragment fragment = new ChooseTimeFragment();
        return fragment;
    }

    @Override
    protected void beforeInitHead() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_choose_time;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        Calendar calendar = Calendar.getInstance();
        // 年
        int yearNow = calendar.get(Calendar.YEAR);
        year.setMinValue(yearNow - 100);
        year.setMaxValue(yearNow + 100);
        year.setValue(yearNow);

        // 月
        String[] monthNum = new String[12];
        for (int i = 0; i < 12; i++) {
            monthNum[i] = (i + 1) + "月";
        }
        month.setMinValue(1);
        month.setMaxValue(monthNum.length);
        month.setDisplayedValues(monthNum);
        month.setValue(calendar.get(Calendar.MONTH) + 1);

        // 日
        day.setMinValue(1);
        int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        String[] newDays = getNewDays(days);
        day.setMaxValue(calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        day.setDisplayedValues(newDays);
        day.setValue(calendar.get(Calendar.DATE));

        // 时
        hour.setMinValue(1);
        int hours = calendar.getActualMaximum(Calendar.HOUR_OF_DAY);
        String[] newHours = getHourAndMinutes(hours, "时");
        hour.setMaxValue(calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
        hour.setDisplayedValues(newHours);
        hour.setValue(calendar.get(Calendar.HOUR_OF_DAY));

        // 时
        minute.setMinValue(1);
        int minutes = calendar.getActualMaximum(Calendar.MINUTE);
        String[] newMinutes = getHourAndMinutes(minutes, "分");
        minute.setMaxValue(calendar.getActualMaximum(Calendar.MINUTE));
        minute.setDisplayedValues(newMinutes);
        minute.setValue(calendar.get(Calendar.MINUTE));

        // 年份和月份更改时对应的天数需要更改
        year.setOnValueChangedListener((picker, oldVal, newVal) -> {
            updateNumberOfDays();
            day.setValue(calendar.get(Calendar.DATE));
        });
        month.setOnValueChangedListener((picker, oldVal, newVal) -> {
            updateNumberOfDays();
            day.setValue(calendar.get(Calendar.DATE));
        });
    }

    private String loadFormatValue(int value) {
        String formatValue = String.valueOf(value);
        if (value < 10) {
            formatValue = "0" + value;
        }
        return formatValue;
    }

    /**
     * 设置选中年份
     *
     * @param yearValue 年
     */
    public void setYearValue(int yearValue) {
        year.setValue(yearValue);
        updateNumberOfDays();
    }

    /**
     * 设置选中月份
     *
     * @param monthValue 月
     */
    public void setMonthValue(int monthValue) {
        month.setValue(monthValue);
        updateNumberOfDays();
    }

    /**
     * 设置选中天数
     *
     * @param dayValue 天
     */
    public void setDayValue(int dayValue) {
        day.setValue(dayValue);
    }

    /**
     * 更新天数
     */
    private void updateNumberOfDays() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year.getValue());
        calendar.set(Calendar.MONTH, (month.getValue() - 1));
        calendar.set(Calendar.DATE, 1);
        calendar.roll(Calendar.DATE, -1);
        int date = calendar.get(Calendar.DATE);
        day.setMaxValue(date);
        day.setDisplayedValues(getNewDays(date));
    }

    /**
     * 格式化天数
     *
     * @param days 天数
     * @return {@link String[]}
     */
    private String[] getNewDays(int days) {
        List<String> dayList = new ArrayList<>();
        for (int i = 0; i < days; i++) {
            dayList.add((i + 1) + "日");
        }
        return dayList.toArray(new String[0]);
    }

    /**
     * 格式化时分数
     *
     * @param count 数量
     * @param des   描述
     * @return 字符穿数组
     */
    private String[] getHourAndMinutes(int count, String des) {
        List<String> dayList = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            dayList.add((i + 1) + des);
        }
        return dayList.toArray(new String[0]);
    }

    @SuppressLint("NotifyDataSetChanged")
    @OnClick({R.id.tv_cancel, R.id.tv_confirm})
    public void onViewClicked(View view) {
        if (view.getId() != R.id.tv_cancel) {
            String show_time = loadFormatValue(year.getValue()) + "-" + loadFormatValue(month.getValue()) + "-" +
                    loadFormatValue(day.getValue()) + " " + loadFormatValue(hour.getValue()) + ":" + loadFormatValue(minute.getValue()) + ":00";
            String time = loadFormatValue(year.getValue()) + loadFormatValue(month.getValue()) +
                    loadFormatValue(day.getValue()) + loadFormatValue(hour.getValue()) + loadFormatValue(minute.getValue()) + "00";
            if (onFunctionListener != null) onFunctionListener.onChoose(show_time,time);
        }
        dismiss();
    }

    public interface OnFunctionListener {
        void onChoose(String show_time, String time);
    }

    public void setOnFunctionListener(OnFunctionListener onFunctionListener) {
        this.onFunctionListener = onFunctionListener;
    }
}